import Data from '@/api/data'
import permission from '@/directive/permission'
import Cookie from 'js-cookie'
import {TokenType} from '@/utils/enum'

const baseURL = process.env.VUE_APP_BASE_API

export default {
  directives: { permission },
  data() {
    return {
      //条件查询数据
      listQuery: {
        page: 1,
        size: 10,
        key: '',
      },

      //中间表格数据
      list: [], // 数据列表
      total: 0, // 数据总数
      listLoading: true, // 加载显示效果 true加载显示 false加载隐藏

      //新增与修改
      drawer: false,
      detailData:{},
      formData: {
        name:"",
      },
      imgError: "",
      imgTypeList:[".jpg",".png"],
      subing: false,
      baseURL: baseURL,

      //新增&修改-编辑器
      editDrawer: false,
      editor: null,
      toolbarConfig: { },
      editorConfig: { placeholder: '请输入内容...' },
      mode: 'default', // or 'simple'

      //文章预览
      drawer3: false,
    }
  },
  created() {
    // 模板渲染前
    if (this.priority) {
      return
    } else {
      this.fetchList()
    }
  },
  methods: {
    aboutPerson(row) {
      this.$router.push("/about/person?parent=" + row.id + "&name=" + row.name)
    
    },
    randomString(length) {
      var str = 'abcdefghijklmnopqrstuvwxyz';
      var result = '';
      for (var i = length; i > 0; --i)
          result += str[Math.floor(Math.random() * str.length)];
      return result;
    },
    selectChange(){
      // 改变分页显示条数始终跳回第一页
      this.listQuery.page = 1
    },
    async fetchList() {
      this.listLoading = true
      const obj = JSON.parse(JSON.stringify(this.listQuery))
      for (const key in obj) {
        if (obj[key] === null || obj[key] === '') {
          delete obj[key]
        }
      }
      obj["type"] = "ABOUT_INFO"
      console.log('wwwwwwwwwwwwwwwwwwwwwwwww')
      console.log(obj)
      const res = await Data.get("/dictionary", obj)
      this.listLoading = false
      if (res) {
        res.list.push({name:"协会负责人", aboutPerson: true})
        this.list = res.list
        this.total = res.total
      }
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    beforeDestroy() {
      const editor = this.editor
      if (editor == null) return
      editor.destroy() // 组件销毁时，及时销毁编辑器
    },
    async deleteById(id) {
      var res = await Data.deleteById("/dictionary/" + id);
      if(res) {
        this.$notify.success({
          message: "院士大师机构删除成功！"
        })
        for(var i = this.list.length-1; i >=0; i--) {
          if(this.list[i].id == id) {
            this.list.splice(i, 1)
          }
        }
      }
    },
    async statusChange(row) {
      var isNormal = row.status.split(":")[0] == 'NORMAL';
      var data = {id: row.id, status: isNormal ? 'DISABLE':'NORMAL'};
      var res = await Data.post("/article/statusChange", data)
      if(res) {
        this.$notify.success({message: "操作成功！"})
        Object.assign(row, {status: isNormal ? 'DISABLE:失效':'NORMAL:正常'})
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    async save() {
      this.subing = true
      //参数转换
      var data = {...this.formData}
      data["type"] = data.type.split(":")[0]
      console.log(this.formData)
      console.log(data)
      var res = await Data.post("/dictionary/update", data);
      if(res) {
        Object.assign(this.currentRow, res);//更新行数据
        this.$notify.success({
          message: '资料修改成功',
        });
      }
      //新增修改统一操作
      this.saveClose()
      this.subing = false
    },
    saveClose() {
      this.formData = {}
      // this.$refs['saveForm'].resetFields();
      this.editDrawer = false
    },
    update(row) {
      this.editDrawer = true;
      this.currentRow = row
      this.formData = {...row}
    },
    imgCheck(file) {
      var maxSize = 1024 * 1024 * 2
      var type = "." + file.name.split(".")[1]
      if (this.imgTypeList.indexOf(type) < 0 || file.size > maxSize) {
        this.imgError = "只能上传2M以内的.jpg/.png图片"
        return false;
      }
      return true;
    },
    imgUpload(response, file, fileList) {
      if(response) {
        console.log("img", response.url)
        this.formData["img"] = response.url
        this.formData["imgList"] = fileList
        this.$refs['saveForm'].validateField("img",()=>{})
      }
    },
    async opened() {
      var node = document.getElementById("articleView");
      node.innerHTML = this.detailData.value
    },
    token() {
      return Cookie.get(TokenType.USER_TOKEN);
    },

  },

}
